//
//  MCMMCViewController.m
//  TLYL
//
//  Created by MC on 2017/7/6.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCMMCViewController.h"
#import "UIImage+Extension.h"

@interface MCMMCViewController ()
{
@private
    ZCSlotMachine *_slotMachine;
    UIButton *_startButton;
    
    UIView *_slotContainerView;
    UIImageView *_slotOneImageView;
    UIImageView *_slotTwoImageView;
    UIImageView *_slotThreeImageView;
    UIImageView *_slotFourImageView;
    UIImageView *_slotFiveImageView;
    NSArray *_slotIcons;
}
@property (nonatomic,strong)NSTimer * timer;
@end


@implementation MCMMCViewController


- (void)viewDidLoad {
    _slotIcons = [NSArray arrayWithObjects:
                  [UIImage drawTextToImage:@"0" addToView:[UIImage imageNamed:@"全选中"]], [UIImage drawTextToImage:@"1" addToView:[UIImage imageNamed:@"全选中"]],[UIImage drawTextToImage:@"2" addToView:[UIImage imageNamed:@"全选中"]],[UIImage drawTextToImage:@"3" addToView:[UIImage imageNamed:@"全选中"]],[UIImage drawTextToImage:@"4" addToView:[UIImage imageNamed:@"全选中"]],[UIImage drawTextToImage:@"5" addToView:[UIImage imageNamed:@"全选中"]],[UIImage drawTextToImage:@"6" addToView:[UIImage imageNamed:@"全选中"]],[UIImage drawTextToImage:@"7" addToView:[UIImage imageNamed:@"全选中"]],[UIImage drawTextToImage:@"8" addToView:[UIImage imageNamed:@"全选中"]],[UIImage drawTextToImage:@"9" addToView:[UIImage imageNamed:@"全选中"]],nil];
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor  whiteColor];
    [self  updatePreferredContentSizeWithTraitCollection:self.traitCollection];
    [self  createUI];

}

-(void)createUI{

    _slotMachine = [[ZCSlotMachine alloc] initWithFrame:CGRectMake(0, 10, 300, 200)];
    _slotMachine.backgroundColor = [UIColor whiteColor];
    _slotMachine.delegate = self;
    _slotMachine.dataSource = self;
    [self.view addSubview:_slotMachine];
    __weak typeof(self) weakSelf = self;
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:weakSelf selector:@selector(start) userInfo:nil repeats:YES];
    
//    int padding = (275-28*5-10*4)/2.0;
//    int width_ball=28;
//    for(int i=0;i<5;i++){
//        UIButton * btn=[[UIButton alloc]init];
//        [btn setBackgroundImage:[UIImage imageNamed:@"goucai_number_selected"] forState:UIControlStateNormal];
//        [self.view addSubview:btn];
//        [btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
//        [btn.titleLabel setTextColor:[UIColor whiteColor]];
//        btn.frame=CGRectMake(padding+i*(width_ball+10), 20, width_ball, width_ball);
//        btn.backgroundColor=[UIColor clearColor];
//        
//    }
//    
    UILabel * lab_title=[[UILabel alloc]init];
    lab_title.text=@"第1次开奖";
    [self.view addSubview:lab_title];
    lab_title.textAlignment=NSTextAlignmentCenter;
    lab_title.frame=CGRectMake(0, 72, 275, 20);
    
    
    UIButton * btn_Stop=[[UIButton alloc]init];
    btn_Stop.backgroundColor=RGB(48, 127, 207);
    [btn_Stop setTitle:@"停止开奖" forState:UIControlStateNormal];
    [btn_Stop.titleLabel setTextColor:[UIColor whiteColor]];
    btn_Stop.layer.cornerRadius=5;
    [btn_Stop addTarget:self action:@selector(action_Stop) forControlEvents:UIControlEventTouchUpInside];
    btn_Stop.clipsToBounds=YES;
    [self.view addSubview:btn_Stop];
    btn_Stop.frame=CGRectMake((275-142)/2.0, 100, 142, 30);
}

#pragma mark-添加定时器
-(void)add_timer{
    _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    [currentRunLoop addTimer:_timer forMode:NSRunLoopCommonModes];
    
}

#pragma mark-停止开奖
-(void)action_Stop{
    
    [self dismissViewControllerAnimated:YES completion:NULL];

}

- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    
    [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
    [self updatePreferredContentSizeWithTraitCollection:newCollection];
    
}
#pragma mark - Private Methods

- (void)start {
    NSUInteger slotIconCount = [_slotIcons count];
    
    NSUInteger slotOneIndex = arc4random()%slotIconCount;
    NSUInteger slotTwoIndex = arc4random()%slotIconCount;
    NSUInteger slotThreeIndex = arc4random()%slotIconCount;
    NSUInteger slotFourIndex = arc4random()%slotIconCount;
    NSUInteger slotFiveIndex = arc4random()%slotIconCount;
    
    _slotOneImageView.image = [_slotIcons objectAtIndex:slotOneIndex];
    _slotTwoImageView.image = [_slotIcons objectAtIndex:slotTwoIndex];
    _slotThreeImageView.image = [_slotIcons objectAtIndex:slotThreeIndex];
    _slotFourImageView.image = [_slotIcons objectAtIndex:slotFourIndex];
    _slotFiveImageView.image = [_slotIcons objectAtIndex:slotFiveIndex];
    _slotMachine.slotResults = [NSArray arrayWithObjects:
                                [NSNumber numberWithInteger:slotOneIndex],
                                [NSNumber numberWithInteger:slotTwoIndex],
                                [NSNumber numberWithInteger:slotThreeIndex],
                                [NSNumber numberWithInteger:slotFourIndex],
                                [NSNumber numberWithInteger:slotFourIndex],
                                nil];
    [_slotMachine startSliding];
}

#pragma mark - UIResponder

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    
    _startButton.highlighted = YES;
    [_startButton performSelector:@selector(setHighlighted:) withObject:[NSNumber numberWithBool:NO] afterDelay:0.8];
    
    [self start];
}

#pragma mark - ZCSlotMachineDelegate

- (void)slotMachineWillStartSliding:(ZCSlotMachine *)slotMachine {
    _startButton.enabled = NO;
}

- (void)slotMachineDidEndSliding:(ZCSlotMachine *)slotMachine {
    _startButton.enabled = YES;
}

#pragma mark - ZCSlotMachineDataSource

- (NSArray *)iconsForSlotsInSlotMachine:(ZCSlotMachine *)slotMachine {
    return _slotIcons;
}

- (NSUInteger)numberOfSlotsInSlotMachine:(ZCSlotMachine *)slotMachine {
    return 5;
}

- (CGFloat)slotWidthInSlotMachine:(ZCSlotMachine *)slotMachine {
    return 60.0f;
}

- (CGFloat)slotSpacingInSlotMachine:(ZCSlotMachine *)slotMachine {
    return 5.0f;
}
#pragma mark-更新高度
- (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection{
    
    self.preferredContentSize = CGSizeMake(300, 150);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
